package GUI;

import beans.User;
import service.CustomerService;
import service.MerchantService;
import service.ProducerService;

import javax.swing.*;
import java.awt.*;

public class loginFrameP extends JFrame {
    JFrame loginFrameP = new JFrame("手机号登录");
    JLabel phoneLabel = new JLabel("手机号：");
    JLabel passwordLabel = new JLabel("密    码：");
    JTextField phoneField = new JTextField(20);
    JTextField passwordField = new JTextField(20);
    JButton loginButtonC = new JButton("顾客登录");
    JButton loginButtonM = new JButton("商家登录");
    JButton loginButtonP = new JButton("生产商登录");
    JButton returnButton = new JButton("返回");
    JPanel phonePanel = new JPanel();
    JPanel passwordPanel = new JPanel();
    JPanel buttonPanel = new JPanel();

    public void loginFrameP(){
        phonePanel.add(phoneLabel);
        phonePanel.add(phoneField);
        passwordPanel.add(passwordLabel);
        passwordPanel.add(passwordField);
        buttonPanel.add(loginButtonC);
        buttonPanel.add(loginButtonM);
        buttonPanel.add(loginButtonP);
        buttonPanel.add(returnButton);

        loginFrameP.add(phonePanel, BorderLayout.NORTH);
        loginFrameP.add(passwordPanel,BorderLayout.CENTER);
        loginFrameP.add(buttonPanel,BorderLayout.SOUTH);
        loginFrameP.setSize(500,200);
        loginFrameP.setLocationRelativeTo(null);
        loginFrameP.setVisible(true);
        loginFrameP.setDefaultCloseOperation(2);

        //顾客登录
        loginButtonC.addActionListener(e -> {
            String phone = phoneField.getText();
            String password = passwordField.getText();

            CustomerService cs = new CustomerService();
            User customer = cs.loginP(phone, password);

            if (customer != null) {
                // 登录成功，弹出新功能菜单
                new CustomerFrame().CustomerFrame(customer);
                loginFrameP.dispose();
            }
        });


        //商家登录
        loginButtonM.addActionListener(e -> {
            String phone = phoneField.getText();
            String password = passwordField.getText();

            MerchantService ms = new MerchantService();
            User merchant = ms.loginP(phone, password);

            if (merchant != null) {
                // 登录成功，弹出新功能菜单
                new MerchantFrame().MerchantFrame(merchant);
                loginFrameP.dispose();
            }
        });

        //生产商登录
        loginButtonP.addActionListener(e -> {
            String phone = phoneField.getText();
            String password = passwordField.getText();

            ProducerService ps = new ProducerService();
            User producer = ps.loginP(phone, password);

            if (producer != null) {
                // 登录成功，弹出新功能菜单
                new ProducerFrame().ProducerFrame(producer);
                loginFrameP.dispose();
            }
        });



        returnButton.addActionListener(e -> {
            loginFrameP.dispose();
            new Menu().Menu();
        });



    }





}
